package gui.nodes;
import gui.AbstractViewPane;
import gui.FormationUIConsulter;
import gui.MainController;
import gui.AbstractViewPane.ViewMode;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import utility.Record;

import business.FormationBL;


public class FormationRoot extends AbstractNode {
	
	public FormationRoot() {
		super();
	}
	
	public String getNodeName() {
		return "Formation";
	}

	public void loadPrimaryViewPane() {
		//choix de ne rien faire sur le clic gauche sur formations
	}
	
	public void loadAjouterViewPane(){
		AbstractViewPane newViewPane = new FormationUIConsulter(this, AbstractViewPane.ViewMode.AJOUT, -1);
		MainController.getInstance().setViewPane(newViewPane);
	}
	
	public JPopupMenu makePopup() {

		return new Popup();
	}
	
	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem ajouter = new JMenuItem("Ajouter");
		
		public Popup() {
			ajouter.addActionListener(this);
			add(ajouter);
		}
		
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == ajouter){
				loadAjouterViewPane();
			}
			
		}
		
	}

	@Override
	public void expandHook() {
		this.removeAllChildren();

		FormationBL formationBL = new FormationBL();
		
		for (Record recordFormation : formationBL.getListeFormation()) {
			FormationNode ensNode = new FormationNode(this, recordFormation);
			add(ensNode);
		}
	}

}
